package com.pzl.cardview;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by zl.peng on 2016-02-24.
 */
public class Adapter extends BaseAdapter {
    private static final String TAG = "Adapter";
    private Context context;

    public Adapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return 12;
    }

    @Override
    public Object getItem(int position) {
        return null;//data=null
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item, null);//instance
            Log.i(TAG, "instance view 解析");
            viewHolder = new ViewHolder(convertView);
        } else {
            viewHolder = ((ViewHolder) convertView.getTag());
            Log.i(TAG, "convertView 重用");
        }
        viewHolder.getTvTitle().setText("Mate " + position);
        return convertView;
    }

    class ViewHolder {
        private TextView tvTitle;
        private ImageView iv;
        private View root;

        public ViewHolder(View convertView) {
            root = convertView;
            convertView.setTag(this);
        }


        public TextView getTvTitle() {
            if (tvTitle == null) {
                tvTitle = (TextView) root.findViewById(R.id.tv_title);
                Log.i(TAG, "get findViewById tvTitle");
            } else {
                Log.i(TAG, "get tvTitle");
            }
            return tvTitle;
        }

        public ImageView getIv() {
            if (iv == null) {
                iv = (ImageView) root.findViewById(R.id.iv);
                Log.i(TAG, "get findViewById iv");
            } else {
                Log.i(TAG, "get iv");
            }
            return iv;
        }
    }
}
